﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Inscriptionem.Services
{
    public static class MailService
    {
        private static SmtpClient Create()
        {
            return new SmtpClient
            {
                Host = Configuration.Configuration.Instance.Mail.Server,
                Port = Configuration.Configuration.Instance.Mail.Port,
                UseDefaultCredentials = false,
                EnableSsl = Configuration.Configuration.Instance.Mail.SSL,
                Credentials = new NetworkCredential(Configuration.Configuration.Instance.Mail.Login, Configuration.Configuration.Instance.Mail.Password)
            };
        }

        public static void SendPasswordReminder(string Mail, Guid GUID)
        {
            var smtp = Create();

            var message = new MailMessage(Configuration.Configuration.Instance.Mail.Sender, Mail)
                              {
                                  Subject = Configuration.Configuration.Instance.Members.Mail.Remind.Subject,
                                  SubjectEncoding = Encoding.UTF8,
                                  IsBodyHtml = true,
                                  Body = Configuration.Configuration.Instance.Members.Mail.Remind.Body.Value.Replace("{Mail}", Mail).Replace("{Token}", GUID.ToString())
                              };

            smtp.Send(message);
        }

        public static void SendMail(string Mail, string Subject, string Message)
        {
            var smtp = Create();

            var message = new MailMessage(Configuration.Configuration.Instance.Mail.Sender, Mail)
            {
                Subject = Subject,
                Body = Message
            };

            smtp.Send(message);
        }
    }
}